﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using backoffice.Data;
using System.Linq.Expressions;

namespace backoffice.Business
{
    public class BusinessRegion
    {
        #region Create
        public static void createRegion(Region r)
        {
            try
            {
                dataRegion.createRegion(r);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating region");
            }
        }


        public static void createRegion(Country c, Region father, String name, int level)
        {
            try
            {
                Region r = new Region();
                r.CountryID = c.CountryID;
                r.Father = father.RegionID;
                r.Name = name;
                r.Level = level;

                dataRegion.createRegion(r);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating region");
            }
        }

        public static void createRegionCoordinates(Region r, Dictionary<float, float> coords)
        {
            try
            {
                List<Coordinate> coordinates = new List<Coordinate>();

                foreach(KeyValuePair<float, float> co in coords){
                    BusinessCoordinate.createCoordinateForRegion(co.Key, co.Value, r);
                }
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating coordinates for region");
            }
        }
        #endregion

        #region Get
        public static Region getRegion(Guid guid)
        {
            try
            {
                return dataRegion.getRegion(guid);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting region");
            }
        }

        public static List<Region> getSubRegions(Region r)
        {
            try
            {
                Expression<Func<Region, bool>> whereClause = (Region re) => re.Father == r.RegionID;

                return dataRegion.getRegions(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting sub-regions");
            }
        }

        public static List<Region> getRegionByCountry(Country c)
        {
            try
            {
                Expression<Func<Region, bool>> whereClause = (Region r) => r.CountryID == c.CountryID;

                return dataRegion.getRegions(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting region by country");
            }
        }

        public static List<Coordinate> getRegionCoordiantes(Region r)
        {
            try
            {
                Expression<Func<Coordinate, bool>> whereClause = (Coordinate c) => (c.RegionID == c.RegionID);

                return dataCoordiante.getCoordinates(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting region coordinates");
            }
        }
        #endregion

        #region Set
        public static Boolean changeRegion(Region r)
        {
            try
            {
                return dataRegion.changeRegion(r);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error changing region");
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteRegion(Region r)
        {
            try
            {
                return dataRegion.deleteRegion(r);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error deletting region");
            }
        }
        #endregion
    }
}
